package com.example.bgImageSear.user.controller;

import com.example.bgImageSear.commons.Result;
import com.example.bgImageSear.user.entity.CommentEntity;
import com.example.bgImageSear.user.service.ICommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController

//评论操作类
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private ICommentService commentService;

    //获取所有评论
    @RequestMapping("/comments")
    public Result list() {
        List<CommentEntity> list = commentService.getCommentList();
        System.out.println(list);
        if (null == list) {
            return new Result(0, "没能获取到所有用户评论", null);
        }
        return new Result(1, "获取到所有用户评论", list);
    }

    //获取相应图片的评论
    @RequestMapping(value = "/commentInfo", method = RequestMethod.POST)
    public Result getCommentListById(@RequestParam int bId) {
        List<CommentEntity> list = commentService.getCommentListById(bId);
        if (null == list) {
            return new Result(0, "获取相应图片的评论失败", null);
        }
        return new Result(1, "获取相应图片的评论成功", list);
    }

    //当前用户评论操作
    @RequestMapping(value = "/commentInsert", method = RequestMethod.POST)
    public Result insertCommentByUserId(@RequestBody CommentEntity commentEntity, @RequestAttribute("uid") Integer uid) {
        commentEntity.setFrom_userId(uid);
        Long number = commentService.insertCommentByUserId(commentEntity);
        if (0 == number) {
            return new Result(0, "评论失败！", null);
        }
        return new Result(1, "评论成功！", number);
    }
}
